import numpy as np
import cv2 as cv


def nothing(x):
    pass


# 创建一个黑色的图像，一个窗口
img = np.zeros((300, 512, 3), np.uint8)
cv.namedWindow("image")
# 创建颜色变化的轨迹栏
cv.createTrackbar("R", "image", 0, 255, nothing)
cv.createTrackbar("G", "image", 0, 255, nothing)
cv.createTrackbar("B", "image", 0, 255, nothing)
# 为 ON/OFF 功能创建开关，只有在该开关为ON的情况下，该应用程序才能在其中运行，否则屏幕始终为黑色
switch = "0 : OFF \n1 : ON"
cv.createTrackbar(switch, "image", 0, 1, nothing)
while 1:
    cv.imshow("image", img)
    k = cv.waitKey(1) & 0xFF
    if k == 27:
        break
    # 得到四条轨迹的当前位置
    r = cv.getTrackbarPos("R", "image")
    g = cv.getTrackbarPos("G", "image")
    b = cv.getTrackbarPos("B", "image")
    s = cv.getTrackbarPos(switch, "image")
    if s == 0:
        img[:] = 0
    else:
        img[:] = [b, g, r]
cv.destroyAllWindows()
